/**
 * FileName：FeignConfiguration.class
 *
 * @author: liujie
 * @date: 2020-04-28 09:50
 */
package com.wangc.commons.configuration;

import com.alibaba.fastjson.JSON;
import com.wangc.commons.configuration.jackson.JacksonCustom;
import feign.Feign;
import feign.RequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Author: liujie
 * @Description: springMvc RequestMapping注解扫描排除FeignClient注解接口
 * @Date:Created in 2020/4/28 9:50.
 * @Modified By:
 */
@Configuration
@ConditionalOnClass({Feign.class})
@Slf4j
public class FeignConfiguration {
    @Bean
    public WebMvcRegistrations feignWebRegistrations() {
        return new WebMvcRegistrations() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return new FeignRequestMappingHandlerMapping();
            }
        };
    }

    @Bean
    public Encoder feignEncoder() {
        return new SpringEncoder(feignHttpMessageConverter());
    }

    @Bean
    public Decoder feignDecoder() {
        return new SpringDecoder(feignHttpMessageConverter());
    }

    //全局统一拦截，用于bs往do层传参数塞head
//    @Bean
//    public RequestInterceptor requestInterceptor() {
//        return requestTemplate -> {
//            ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//            if (attrs != null) {
//                //如果用户不为空，则新增到header
//                if(null != UserThreadLocal.getUser()){
//                    try {
//                        requestTemplate.header("user", URLEncoder.encode(JSON.toJSONString(UserThreadLocal.getUser()),"utf-8"));
//                    } catch (UnsupportedEncodingException e) {
//                        log.error("user encode error",e);
//                    }
//                }
//            }
//        };
//    }

    /**
     * 设置解码器为fastjson
     *
     * @return
     */
    private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
        final HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(
                new JacksonCustom().customObjectMapper());
        return () -> new HttpMessageConverters(jacksonConverter);
    }

    private static class FeignRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
        @Override
        protected boolean isHandler(Class<?> beanType) {
            return super.isHandler(beanType) &&
                    !AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class);
        }
    }
}
